<template>
  <div class="ip-blacklist">
    <div class="header">
      <h2>IP黑名单管理</h2>
      <el-button type="primary" @click="handleAddClick">添加IP</el-button>
    </div>

    <el-table :data="blacklistData" style="width: 100%">
      <el-table-column prop="ip" label="IP地址" width="200"></el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button
              type="danger"
              size="small"
              @click="removeIp(scope.row)"
          >
            移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加IP对话框 -->
    <el-dialog :visible.sync="showAddDialog" title="添加IP到黑名单" width="400px">
      <el-form :model="addForm" label-width="80px">
        <el-form-item label="IP地址" required>
          <el-input
              v-model="addForm.ipAddress"
              placeholder="请输入IP地址"
          ></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="showAddDialog = false">取消</el-button>
        <el-button type="primary" @click="addIp">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'IpBlacklist',
  data() {
    return {
      blacklistData: [],
      showAddDialog: false,
      addForm: {
        ipAddress: ''
      }
    }
  },
  mounted() {
    console.log('IpBlacklist组件已挂载')
    this.loadBlacklist()
  },
  methods: {
    handleAddClick() {
      console.log('添加IP按钮被点击了')
      console.log('当前showAddDialog值:', this.showAddDialog)
      this.showAddDialog = true
      console.log('设置后showAddDialog值:', this.showAddDialog)
    },

    async loadBlacklist() {
      try {
        const response = await axios.get('/api/blacklist')
        this.blacklistData = response.data.data.map(ip => ({ ip }))
      } catch (error) {
        this.$message.error('加载黑名单失败: ' + error.response?.data?.message)
      }
    },

    async addIp() {
      console.log('addIp方法被调用')
      console.log('输入的IP:', this.addForm.ipAddress)

      if (!this.addForm.ipAddress || !this.addForm.ipAddress.trim()) {
        this.$message.warning('请输入IP地址')
        return
      }

      try {
        const response = await axios.post('/api/blacklist', {
          ipAddress: this.addForm.ipAddress.trim()
        })

        this.$message.success('IP已添加到黑名单')
        this.showAddDialog = false
        this.addForm.ipAddress = ''
        this.loadBlacklist()
      } catch (error) {
        console.error('添加IP失败:', error)
        this.$message.error('添加失败: ' + (error.response?.data?.message || error.message))
      }
    },

    async removeIp(row) {
      try {
        await this.$confirm('确定要从黑名单中移除此IP吗？', '确认', {
          type: 'warning'
        })

        await axios.delete('/api/blacklist', {
          params: { ipAddress: row.ip }
        })

        this.$message.success('IP已从黑名单移除')
        this.loadBlacklist()
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('移除失败: ' + error.response?.data?.message)
        }
      }
    }
  }
}
</script>

<style scoped>
.ip-blacklist {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>